//
//  ST_Zodiac.swift
//  BTVideo
//
//  Created by DouLei on 2019/9/27.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit

//这里面少了 摩羯座 没有查询到就是摩羯座
fileprivate var foo:[((Int, Int), (Int, Int))] = {
     return [((1, 20), (2, 18)),
             ((2, 19), (3, 20)),
             ((3, 21), (4, 19)),
             ((4, 20), (5, 20)),
             ((5, 21), (6, 20)),
             ((6, 21), (7, 22)),
             ((7, 23), (8, 22)),
             ((8, 23), (9, 22)),
             ((9, 23), (10, 22)),
             ((10, 23), (11, 21)),
             ((11, 22), (12, 21))
    ]
}()

enum ST_Zodiac: Int {
    case 水瓶座 = 0, 双鱼座,白羊座, 金牛座, 双子座, 巨蟹座, 狮子座, 处女座, 天秤座, 天蝎座, 射手座,摩羯座
    
    func displayString() -> String {
        return ["水瓶座","双鱼座","白羊座", "金牛座","双子座","巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座","摩羯座"][self.rawValue]
    }
    
    func displayIcon()->String?{
        return ST_ZodiacIcons[safe:self.rawValue]?.icon
    }
}

fileprivate var ST_ZodiacIcons = [ModifyUserItemCellModel]()




extension ST_Zodiac{
    
    static func pull_s_t__zodiac_icons(){
        
        JVrequest(PH.fetchOptionTagsApi,parameters:["search_key":"constellation"], success: { (json) in
            
            if let list = [ModifyUserItemCellModel].deserialize(from: json["data"].rawString() ?? "") as? [ModifyUserItemCellModel]{
                ST_ZodiacIcons = list
            }
            
        }) { (code, err) in
            
            print("拉取星座ICON失败------,",err)
            
        }
        
    }
    
    static func calculate_s_t__zodiac(monthStr:Int,dayStr:Int)->ST_Zodiac?{
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        //        使用  闰年 来计算区间 避免 2月29日 等特殊问题
        let assistDateStr = String.init(format: "2000-%02d-%2d", monthStr,dayStr)
        
        guard let assistDate = dateFormatter.date(from: assistDateStr) else{
            print("传递的月份和日错误")
            return nil
        }
        
        for (idx,item) in foo.enumerated(){
            
            let c1Str = String.init(format: "2000-%02d-%2d", item.0.0,item.0.1)
            
            
            let c2Str = String.init(format: "2000-%02d-%2d", item.1.0,item.1.1)
            
            guard let date1 = dateFormatter.date(from: c1Str), let date2 = dateFormatter.date(from: c2Str)else {
                return nil
            }
            
            let range = date1...date2
            
            if range.contains(assistDate){
                return ST_Zodiac.init(rawValue: idx)
            }
        }
//      区间内没查询到 就是 摩羯座
        return ST_Zodiac.init(rawValue: 11)
    }
}
